package com.medical.service;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical.config.SystemContext;
import com.medical.dao.SysUserMapper;
import com.medical.entity.SysUser;

@Service
@Transactional
public class UserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	
	/**
	 * 根据用户ID查找用户信息
	 * @param userId
	 * @return
	 */
	public SysUser findUserByUserId(Integer userId) {
		return sysUserMapper.findUserByUserId(userId);
	}
	
	/**
	 * 查找用户列表
	 * @return
	 */
	public PageInfo<SysUser> getUserList(String userName, Integer currentPage, Integer limit){
		//利用PageHelper分页查询 注意：这个一定要放查询语句的前一行,否则无法进行分页,因为它对紧随其后第一个sql语句有效
        PageHelper.startPage(currentPage, limit);
        List<SysUser> userList = sysUserMapper.getUserList(userName);
        PageInfo<SysUser> pageInfo = new PageInfo<SysUser>(userList);
		return pageInfo;
	}
	
	/**
	 * 根据ID获取用户信息
	 * @param id
	 * @return
	 */
	public SysUser getUserById(Integer id) {
		return sysUserMapper.selectByPrimaryKey(id);
	}
	
	/**
	 * 添加
	 * @param sysUser
	 */
	public void addUser(SysUser sysUser) {
		sysUser.setCreateTime(new Timestamp(System.currentTimeMillis()));
		sysUser.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		
		sysUser.setUserPassword(SystemContext.passwdEncryption(sysUser.getUserPassword()));
		
		sysUserMapper.insert(sysUser);
	}
	
	/**
	 * 更新
	 * @param sysUser
	 */
	public void edit(SysUser sysUser) {
		sysUser.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		sysUserMapper.updateByPrimaryKeySelective(sysUser);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void delete(Integer id) {
		sysUserMapper.deleteByPrimaryKey(id);
	}

}
